<template>
  <div id="contentBody" class="box-body">
    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">在职培训情况</h3>
        <div class="box-tools pull-right">
          <button type="button" v-if='isEditable' class="btn btn btn-success" @click="()=>addVisible = true">
              <i class='fa fa-plus-square-o'>添加培训信息</i>
            </button>
        </div>
      </div>
      <div class="box-body">
        <div class=" form-inline dt-bootstrap" id="example1_wrapper">
          <div class="row">
            <div class="col-sm-12 table-responsive">
              <table class="table table-hover table table-bordered table-striped dataTable">
                <tbody>
                  <tr>
                    <th aria-label="起始时间" style="width: 142px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">起始时间</th>
                    <th aria-label="结束时间" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">结束时间</th>
                    <th aria-label="培训类别" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">培训类别</th>
                    <th aria-label="培训名称" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">培训名称</th>
                    <th aria-label="主办单位" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">主办单位</th>
                    <th aria-label="学习内容" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">学习内容</th>
                    <th aria-label="课时" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">课时</th>
                    <th aria-label="成绩" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">成绩</th>
                    <th aria-label="备注" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">备注</th>
                    <th v-if='isEditable' aria-label="操作" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">操作</th>
                  </tr>
                  <tr :key='index' v-for='(value,index) in this.trainings' role="row" class="value.cls">
                    <td> {{ value.starttime }}</td>
                    <td>{{ value.endtime }}</td>
                    <td class="text-light" v-text="getcategoryname(value.category)"></td>
                    <td>{{ value.traingname }}</td>
                    <td>{{ value.agency }}</td>
                    <td>{{ value.course }}</td>
                    <td>{{ value.hours }}</td>
                    <td>{{ value.score }}</td>
                    <td>{{ value.comment }}</td>
                    <td v-if='isEditable'>
                      <div class="col-md-2">
                        <button @click="deletetrain(index)" class="btn btn-xs">删除</button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      <el-dialog id='modaltraining' title='新增培训信息' v-if="addVisible" :visible.sync="addVisible" :before-close="()=>addVisible = false" :modal="false" size="small">
        <div class="form-horizontal">
          <div class="box-body">
            <div class="form-group">
              <label class="col-md-2 control-label my-label">起始时间</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editTraing.starttime" @timeChanged="time=>editTraing.starttime = time"></MyDatePicker>
              </div>
              <label class="col-md-2 control-label my-label">结束时间</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editTraing.endtime" @timeChanged="time=>editTraing.endtime = time"></MyDatePicker>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">培训类别</label>
              <div class="col-sm-4">
                <select v-model="editTraing.category" class="form-control">
                    <option :key='option.id' v-for="option in this.traingcategorylist" v-bind:value="option.id">{{ option.name }}</option>
                  </select>
              </div>
              <label class="col-md-2 control-label my-label">培训名称</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editTraing.traingname" placeholder="请输入培训名称">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">主办单位</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editTraing.agency" placeholder="请输入主办单位">
              </div>
              <label class="col-md-2 control-label my-label">学习内容</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editTraing.course" placeholder="请输入学习内容">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">课时</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editTraing.hours" placeholder="请输入课时">
              </div>
              <label class="col-md-2 control-label my-label">成绩</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editTraing.score" placeholder="请输入成绩">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">备注</label>
              <div class="col-sm-9">
                <input class="form-control" v-model="editTraing.comment" placeholder="请输入备注">
              </div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import MyDatePicker from '../../mycomponents/common/MyDatePicker'
  export default {
    name: 'Traing',
    props: ['trainings', 'editable'],
    data: function() {
      return {
        //trainings: [],
        isEditable: true,
        addVisible: false,
        traingcategorylist: [],
        editTraing: {
          starttime: '',
          endtime: '',
          category: 1,
          traingname: '',
          agency: '',
          course: '',
          hours: 0,
          score: 100,
          comment: ''
        }
      }
    },
    created: function() {
      if (this.editable != null) {
        this.isEditable = this.editable
      }
      this.traingcategorylist = this.$store.state.sysconfigs.categorylist
    },
    components: {
      MyDatePicker
    },
    methods: {
      clearEditData() {
        this.editTraing.endtime = '',
        this.editTraing.starttime = '',
        this.editTraing.category = 1,
        this.editTraing.traingname = '',
        this.editTraing.agency = '',
        this.editTraing.course = '',
        this.editTraing.hours = 0,
        this.editTraing.score = 100,
        this.editTraing.comment = ''
      },
      getcategoryname(id) {
        for (var index in this.traingcategorylist) {
          if (this.traingcategorylist[index].id == id) {
            return this.traingcategorylist[index].name;
          }
        }
        return ''
      },
      deletetrain(index) {
        this.trainings.splice(index, 1)
      },
      deepCopy(o, c) {
        var c = c || {}
        for (var i in o) {
          if (typeof o[i] === 'object') {
            if (o[i].constructor === Array) {
              c[i] = []
            } else {
              c[i] = {}
            }
            this.deepCopy(o[i], c[i])
          } else {
            c[i] = o[i]
          }
        }
        return c
      },
      getData() {
        return this.trainings
      },
      submit(e) {
        var newtrain = {}
        this.deepCopy(this.editTraing, newtrain)
        this.trainings.push(newtrain)
        this.clearEditData()
        this.addVisible = false
      }
    }
  }
</script>

<style scoped>
  #contentBody {
    min-height: 400px;
  }
</style>